package com.draby.starfantasymaze.activities.main.fragments;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.draby.starfantasymaze.R;
import com.draby.starfantasymaze.methods.game.CreateGame;
import com.draby.starfantasymaze.activities.game.GameSceneActivity;
import com.draby.starfantasymaze.dto.map.Map;
import com.draby.starfantasymaze.enums.Customize;
import com.draby.starfantasymaze.enums.Mode;
import com.draby.starfantasymaze.utils.DateFormatUtil;
import com.draby.starfantasymaze.utils.FileUtil;
import com.draby.starfantasymaze.utils.LunarCalendarUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class HomeFragment extends Fragment {

    // 文件存放父目录与文件名
    private String parent;
    private String data_fileName;

    // 可能用到的控件
    private Button btn_easy;
    private Button btn_normal;
    private Button btn_hard;
    private Button btn_infinite;
    private Button btn_birthday;
    private LinearLayout ll_birthday;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 设置文件存放父目录与文件名
        parent = getString(R.string.file_directory);
        data_fileName = getString(R.string.fileName_data);

        // 找到控件
        btn_easy = view.findViewById(R.id.btn_easy);
        btn_normal = view.findViewById(R.id.btn_normal);
        btn_hard = view.findViewById(R.id.btn_hard);
        btn_infinite = view.findViewById(R.id.btn_infinite);
        btn_birthday = view.findViewById(R.id.btn_birthday);
        // 控制节日控件显隐的布局容器
        ll_birthday = view.findViewById(R.id.ll_birthday);

        // 设置按钮的可见性
        setVisibility();
        
        // 设置按钮点击事件监听器
        setOnClickListener();
    }

    /**
     * 设置按钮点击事件监听器
     */
    private void setOnClickListener() {
        OnClick onClick = new OnClick();
        btn_easy.setOnClickListener(onClick);
        btn_normal.setOnClickListener(onClick);
        btn_hard.setOnClickListener(onClick);
        btn_infinite.setOnClickListener(onClick);
        btn_birthday.setOnClickListener(onClick);
    }

    /**
     * 设置节日按钮是否可见
     */
    private void setVisibility() {
        checkBirthday();
    }

    /**
     * 判断生日地图按钮是否可见
     */
    private void checkBirthday() {
        try {
            int month, day, lunarMonth, lunarDay, nowMonth, nowDay, nowLunarMonth, nowLunarDay;
            SimpleDateFormat formatter = DateFormatUtil.getFormatter("yyyy-MM-dd");
            String strBirthday = FileUtil.getValue(parent, data_fileName, "birthday");
            if (strBirthday == null) {
                ll_birthday.setVisibility(View.GONE);
                return;
            }
            Date birthday = formatter.parse(strBirthday);
            if (birthday == null) {
                ll_birthday.setVisibility(View.GONE);
                return;
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(birthday);
            month = calendar.get(Calendar.MONTH) + 1;
            day = calendar.get(Calendar.DAY_OF_MONTH);
            LunarCalendarUtil lunar = new LunarCalendarUtil(calendar);
            lunarMonth = lunar.getMonth();
            lunarDay = lunar.getDay();
            calendar = Calendar.getInstance();
            nowMonth = calendar.get(Calendar.MONTH) + 1;
            nowDay = calendar.get(Calendar.DAY_OF_MONTH);
            lunar = new LunarCalendarUtil(calendar);
            nowLunarMonth = lunar.getMonth();
            nowLunarDay = lunar.getDay();
            if ((month == nowMonth && day == nowDay) || (lunarMonth == nowLunarMonth && lunarDay == nowLunarDay)) {
                ll_birthday.setVisibility(View.VISIBLE);
            } else {
                ll_birthday.setVisibility(View.GONE);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            ll_birthday.setVisibility(View.GONE);
        }
    }

    /**
     * fragment 切换时执行
     * @param hidden true: fragment 被隐藏
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            setVisibility();
        }
    }

    /**
     * 点击事件实现类，所有按钮点击事件在此实现
     */
    private class OnClick implements View.OnClickListener {
        @SuppressLint("NonConstantResourceId")
        @Override
        public void onClick(View v) {
            CreateGame createGame = null;
            Map map;
            switch (v.getId()) {
                // 简单模式
                case R.id.btn_easy:
                    createGame = new CreateGame(Mode.EASY);
                    break;
                // 普通模式
                case R.id.btn_normal:
                    createGame = new CreateGame(Mode.NORMAL);
                    break;
                // 困难模式
                case R.id.btn_hard:
                    createGame = new CreateGame(Mode.HARD);
                    break;
                // 无尽模式
                case R.id.btn_infinite:
                    createGame = new CreateGame(Mode.RANDOM);
                    break;
                // 生日地图
                case R.id.btn_birthday:
                    createGame = new CreateGame(Customize.BIRTHDAY);
                    break;
            }
            if (createGame != null) {
                // 根据不同模式来创建游戏地图
                map = createGame.createGame();
                // 判断地图是否创建成功
                if (map != null) {
                    // 创建成功则跳转到游戏界面，并将游戏地图传送到对应 activity
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("Map", map);
                    bundle.putSerializable("Mode", createGame.getMode());
                    bundle.putSerializable("Customize", createGame.getCustomize());
                    Intent intent = new Intent(getContext(), GameSceneActivity.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    // 失败则给出提示
                    Toast.makeText(getContext(), "地图创建失败，请重新尝试！", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
